package cn.wrl.cart.controller;

import cn.wrl.cart.service.ShopCartService;
import cn.wrl.controller.BaseController;
import cn.wrl.pojo.IMOOCJSONResult;
import cn.wrl.pojo.ShopcartBO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 购物车
 */
@Slf4j
@Api(value = "购物车", tags = {"购物车接口相关的api"})
@RestController
@RequestMapping("shopcart")
public class CartController extends BaseController {

    @Autowired
    private ShopCartService shopCartService;

    @ApiOperation(value = "添加商品到购物车", httpMethod = "POST")
    @PostMapping("add")
    public IMOOCJSONResult carousel(@RequestParam String userId,
                                    @RequestBody ShopcartBO shopcartBO) {
        log.info("{}", shopcartBO);
        if (StringUtils.isBlank(userId)) {
            return IMOOCJSONResult.errorMsg("");
        }

        return shopCartService.add(userId, shopcartBO);
    }

    @ApiOperation(value = "删除购物车商品", httpMethod = "POST")
    @PostMapping("del")
    public IMOOCJSONResult del(@RequestParam String userId,
                               @RequestParam String itemSpecId) {
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(itemSpecId)) {
            return IMOOCJSONResult.errorMsg("");
        }
        return shopCartService.del(userId, itemSpecId);
    }

    @ApiOperation(value = "清空购物车", httpMethod = "POST")
    @PostMapping("clear")
    public IMOOCJSONResult clear(@RequestParam String userId) {
        if (StringUtils.isBlank(userId)) {
            return IMOOCJSONResult.errorMsg("");
        }
        return shopCartService.clear(userId);
    }



}
